﻿
// MFCApplication2Dlg.cpp: 实现文件
//

#include "pch.h"
#include "framework.h"
#include "MFCApplication2.h"
#include "MFCApplication2Dlg.h"
#include "afxdialogex.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// 用于应用程序“关于”菜单项的 CAboutDlg 对话框

class CAboutDlg : public CDialogEx
{
public:
	CAboutDlg();

// 对话框数据
#ifdef AFX_DESIGN_TIME
	enum { IDD = IDD_ABOUTBOX };
#endif

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持

// 实现
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()


// CMFCApplication2Dlg 对话框



CMFCApplication2Dlg::CMFCApplication2Dlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_MFCAPPLICATION2_DIALOG, pParent)
	, m_str(_T(""))
{
	m_str = "0";
	m_end = true;
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMFCApplication2Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_DISPLAY, m_str);
}

BEGIN_MESSAGE_MAP(CMFCApplication2Dlg, CDialogEx)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_NUM_0, &CMFCApplication2Dlg::OnBnClickedNum0)
	ON_BN_CLICKED(IDC_NUM_1, &CMFCApplication2Dlg::OnBnClickedNum1)
	ON_BN_CLICKED(IDC_NUM_2, &CMFCApplication2Dlg::OnBnClickedNum2)
	ON_BN_CLICKED(IDC_NUM_3, &CMFCApplication2Dlg::OnBnClickedNum3)
	ON_BN_CLICKED(IDC_NUM_4, &CMFCApplication2Dlg::OnBnClickedNum4)
	ON_BN_CLICKED(IDC_NUM_5, &CMFCApplication2Dlg::OnBnClickedNum5)
	ON_BN_CLICKED(IDC_NUM_6, &CMFCApplication2Dlg::OnBnClickedNum6)
	ON_BN_CLICKED(IDC_NUM_7, &CMFCApplication2Dlg::OnBnClickedNum7)
	ON_BN_CLICKED(IDC_NUM_8, &CMFCApplication2Dlg::OnBnClickedNum8)
	ON_BN_CLICKED(IDC_NUM_9, &CMFCApplication2Dlg::OnBnClickedNum9)
	ON_BN_CLICKED(IDC_POINT, &CMFCApplication2Dlg::OnBnClickedPoint)
	ON_BN_CLICKED(IDC_CLEAR, &CMFCApplication2Dlg::OnBnClickedClear)
	ON_BN_CLICKED(IDC_ADD, &CMFCApplication2Dlg::OnBnClickedAdd)
	ON_BN_CLICKED(IDC_SUBSTRACT, &CMFCApplication2Dlg::OnBnClickedSubstract)
	ON_BN_CLICKED(IDC_MULTIPLY, &CMFCApplication2Dlg::OnBnClickedMultiply)
	ON_BN_CLICKED(IDC_DIVIDE, &CMFCApplication2Dlg::OnBnClickedDivide)
	ON_BN_CLICKED(IDC_SQUARED, &CMFCApplication2Dlg::OnBnClickedSquared)
	ON_BN_CLICKED(IDC_SQUARE, &CMFCApplication2Dlg::OnBnClickedSquare)
	ON_BN_CLICKED(IDC_EQUAL, &CMFCApplication2Dlg::OnBnClickedEqual)
	ON_BN_CLICKED(IDC_BACK, &CMFCApplication2Dlg::OnBnClickedBack)
END_MESSAGE_MAP()


// CMFCApplication2Dlg 消息处理程序

BOOL CMFCApplication2Dlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// 将“关于...”菜单项添加到系统菜单中。

	// IDM_ABOUTBOX 必须在系统命令范围内。
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != nullptr)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 设置此对话框的图标。  当应用程序主窗口不是对话框时，框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	// TODO: 在此添加额外的初始化代码

	return TRUE;  // 除非将焦点设置到控件，否则返回 TRUE
}

void CMFCApplication2Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialogEx::OnSysCommand(nID, lParam);
	}
}

// 如果向对话框添加最小化按钮，则需要下面的代码
//  来绘制该图标。  对于使用文档/视图模型的 MFC 应用程序，
//  这将由框架自动完成。

void CMFCApplication2Dlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作区矩形中居中
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialogEx::OnPaint();
	}
}

//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CMFCApplication2Dlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}




/**
*   接下来数字按键、符号按键显示的逻辑都相似：
*   m_end表示一次计算结束，如果结束了，那么输入的第一个字符就会把"0"替换为该数字;否则是在"0"后加该数字
*   使用UpdateData及时更新，在edit控件里显示
* 	m_num记录操作数，当用户输入运算符，m_num就会把值赋给m_num1并清零，用户输入等于，m_num把值赋给m_num2清零
* 	m_flag记录运算符号，1~6分别是加、减、乘、除、乘方、开方，其中5和6是单目运算符
*/
void CMFCApplication2Dlg::OnBnClickedNum0()
{
	UpdateData(TRUE);
	if (m_end) {
		m_num = "0";
		m_str = "0";
		m_end = false;
	}
	else {
		m_num += "0";
		m_str += "0";
	}
	UpdateData(FALSE);
}


void CMFCApplication2Dlg::OnBnClickedNum1()
{
	UpdateData(TRUE);
	if (m_end) {
		m_num = "1";
		m_str = "1";
		m_end = false;
	}
	else {
		m_num += "1";
		m_str += "1";
	}
	UpdateData(FALSE);
}


void CMFCApplication2Dlg::OnBnClickedNum2()
{
	UpdateData(TRUE);
	if (m_end) {
		m_num = "2";
		m_str = "2";
		m_end = false;
	}
	else {
		m_num += "2";
		m_str += "2";
	}
	UpdateData(FALSE);
}


void CMFCApplication2Dlg::OnBnClickedNum3()
{
	UpdateData(TRUE);
	if (m_end) {
		m_num = "3";
		m_str = "3";
		m_end = false;
	}
	else {
		m_num += "3";
		m_str += "3";
	}
	UpdateData(FALSE);
}


void CMFCApplication2Dlg::OnBnClickedNum4()
{
	UpdateData(TRUE);
	if (m_end) {
		m_num = "4";
		m_str = "4";
		m_end = false;
	}
	else {
		m_num += "4";
		m_str += "4";
	}
	UpdateData(FALSE);
}


void CMFCApplication2Dlg::OnBnClickedNum5()
{
	UpdateData(TRUE);
	if (m_end) {
		m_num = "5";
		m_str = "5";
		m_end = false;
	}
	else {
		m_num += "5";
		m_str += "5";
	}
	UpdateData(FALSE);
}


void CMFCApplication2Dlg::OnBnClickedNum6()
{
	UpdateData(TRUE);
	if (m_end) {
		m_num = "6";
		m_str = "6";
		m_end = false;
	}
	else {
		m_num += "6";
		m_str += "6";
	}
	UpdateData(FALSE);
}


void CMFCApplication2Dlg::OnBnClickedNum7()
{
	UpdateData(TRUE);
	if (m_end) {
		m_num = "7";
		m_str = "7";
		m_end = false;
	}
	else {
		m_num += "7";
		m_str += "7";
	}
	UpdateData(FALSE);
}


void CMFCApplication2Dlg::OnBnClickedNum8()
{
	UpdateData(TRUE);
	if (m_end) {
		m_num = "8";
		m_str = "8";
		m_end = false;
	}
	else {
		m_num += "8";
		m_str += "8";
	}
	UpdateData(FALSE);
}


void CMFCApplication2Dlg::OnBnClickedNum9()
{
	UpdateData(TRUE);
	if (m_end) {
		m_num = "9";
		m_str = "9";
		m_end = false;
	}
	else {
		m_num += "9";
		m_str += "9";
	}
	UpdateData(FALSE);
}

//小数点
void CMFCApplication2Dlg::OnBnClickedPoint()
{
	UpdateData(TRUE);
	//使用find，是防止用户在一个数字中输入2个及以上的小数点。
	//m_flag是运算符，没有输入时是0，它能表明用户在输入第一个数还是第二个数
	//(如果不根据flag分情况讨论,那么用户在第一个数输入小数点后，在第二个数就不能输入了)
	if (!m_flag) {
		if (m_str.Find(L'.') == -1) {
			m_num += ".";
			m_str += ".";
		}
	}
	else
		if (m_num.Find(L'.') == -1) {
			m_num += ".";
			m_str += ".";
		}
	UpdateData(FALSE);
}

//清空，要注意设置结束标志m_end
void CMFCApplication2Dlg::OnBnClickedClear()
{
	UpdateData(TRUE);
	m_num = "0";
	m_str = "0";
	m_end = true;
	UpdateData(FALSE);
}

//加法，用m_flag记录运算符
void CMFCApplication2Dlg::OnBnClickedAdd()
{
	UpdateData(TRUE);
	m_num1 = _wtof(m_num);
	m_num = "";
	m_str += "+";
	m_flag = 1;
	UpdateData(FALSE);
}

//减法
void CMFCApplication2Dlg::OnBnClickedSubstract()
{
	UpdateData(TRUE);
	m_num1 = _wtof(m_num);
	m_num = "";
	m_str += "-";
	m_flag = 2;
	UpdateData(FALSE);
}

//乘法
void CMFCApplication2Dlg::OnBnClickedMultiply()
{
	UpdateData(TRUE);
	m_num1 = _wtof(m_num);
	m_num = "";
	m_str += "×";
	m_flag = 3;
	UpdateData(FALSE);
}

//除法
void CMFCApplication2Dlg::OnBnClickedDivide()
{
	UpdateData(TRUE);
	m_num1 = _wtof(m_num);
	m_num = "";
	m_str += "÷";
	m_flag = 4;
	UpdateData(FALSE);
}

//乘方
void CMFCApplication2Dlg::OnBnClickedSquared()
{
	UpdateData(TRUE);
	m_num1 = _wtof(m_num);
	m_num = "";
	m_str += "^2";
	m_flag = 5;
	UpdateData(FALSE);
}

//开方
void CMFCApplication2Dlg::OnBnClickedSquare()
{
	UpdateData(TRUE);
	m_num1 = _wtof(m_num);
	m_num = "";
	//需要在操作数之前加sqrt标识
	CString temp("sqrt(");
	m_str = temp + m_str;
	m_str += ")";
	m_flag = 6;
	UpdateData(FALSE);
}


void CMFCApplication2Dlg::OnBnClickedEqual()
{
	//没有运算符，结果为自身
	if (m_flag == 0) {
		m_result = _wtof(m_str);
	}
	//乘方运算
	if (m_flag == 5) {
		m_result = m_num1 * m_num1;
	}
	//开方运算
	else if (m_flag == 6) {
		m_result = sqrt(m_num1);
	}
	//加减乘除，根据flag判断，在此之前先将第二个数存储
	else {
		m_num2 = _wtof(m_num);
		switch (m_flag) {
		case 1:
			m_result = m_num1 + m_num2;
			break;
		case 2:
			m_result = m_num1 - m_num2;
			break;
		case 3:
			m_result = m_num1 * m_num2;
			break;
		case 4:
			m_result = m_num1 / m_num2;
			break;
		default:break;
		}
	}
	//计算完之后操作数要清空
	m_num = "";
	m_num2 = 0;
	m_flag = 0;
	m_end = true;
	UpdateData(TRUE);
	//如果是很接近整数的小数，就约为整数
	if (m_result - (float)(int(m_result)) < 1e-3)
		m_str.Format(L"%d", (int)m_result);

	else
		m_str.Format(_T("%.2f"), m_result);
	UpdateData(FALSE);
}

//后退
void CMFCApplication2Dlg::OnBnClickedBack()
{
	UpdateData(TRUE);
	//取左边的长度减一个字符
	if(!m_str.IsEmpty())
		m_str = m_str.Left(m_str.GetLength()-1);
	UpdateData(FALSE);
}


BOOL CMFCApplication2Dlg::PreTranslateMessage(MSG* pMsg)
{
	//实现小键盘输入
	if (pMsg->message == WM_KEYDOWN) {
		switch (pMsg->wParam) {
		case VK_RETURN:
			OnBnClickedEqual();
			break;
		case VK_BACK:
			OnBnClickedBack();
			break;
		case VK_ADD:
			OnBnClickedAdd();
			break;
		case VK_SUBTRACT:
			OnBnClickedSubstract();
			break;
		case VK_MULTIPLY:
			OnBnClickedMultiply();
			break;
		case VK_DIVIDE:
			OnBnClickedDivide();
			break;
		case VK_DECIMAL:
			OnBnClickedPoint();
			break;
		case VK_NUMPAD0:
			OnBnClickedNum0();
			break;
		case VK_NUMPAD1:
			OnBnClickedNum1();
			break;
		case VK_NUMPAD2:
			OnBnClickedNum2();
			break;
		case VK_NUMPAD3:
			OnBnClickedNum3();
			break;
		case VK_NUMPAD4:
			OnBnClickedNum4();
			break;
		case VK_NUMPAD5:
			OnBnClickedNum5();
			break;
		case VK_NUMPAD6:
			OnBnClickedNum6();
			break;
		case VK_NUMPAD7:
			OnBnClickedNum7();
			break;
		case VK_NUMPAD8:
			OnBnClickedNum8();
			break;
		case VK_NUMPAD9:
			OnBnClickedNum9();
			break;
		case VK_CONTROL:
			OnBnClickedClear();
			break;
		}
	}
	if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_ESCAPE) return TRUE;//屏蔽esc键
	if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_RETURN) return TRUE;//屏蔽回车键，让他不再退出

	return CDialogEx::PreTranslateMessage(pMsg);
}
